package com.wondertek.poms.pre.manager.xml2Cont;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.core.service.IPlaybillService;
import com.wondertek.poms.core.service.ISystemConfigService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.Playbill;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class PlayBillManager {
    @Autowired
    private IPlaybillService playbillService;
    @Autowired
    ISystemConfigService systemConfigService;
	
	public void process(Content c) throws Exception {
		try {
            List<Playbill> pbs = playbillService.findByContId(c.getContentId());
            if (pbs != null && pbs.size() > 0) {
                playbillService.deleteAll(c.getContentId());
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
            String type = StringUtil.null2Str(c.getType());
            //formType等于13时，自己就是直播节目单
            if ((type.equals("2") || type.equals("5") || type.equals("8") || type.equals("9") || type.equals("12")
                    || StringUtil.null2Str(c.getDisplayType()).equals("500020")) && !"13".equals(c.getFormType())) {
                String contentId = c.getContentId();
                String path = systemConfigService.findAllBykey("syncFile_basePath") + "/" + c.getPlaybillFilePath();
                File pbFile = new File(path);
                if (pbFile.exists()) {
                    try {
                        SAXReader sr = new SAXReader();
                        Document doc = sr.read(pbFile);
                        Element root = doc.getRootElement();
                        List PlaybillList = root.elements("Playbill");
                        if (PlaybillList != null && PlaybillList.size() > 0) {
                            List<Playbill> dbpbs = new ArrayList<Playbill>();
                            Iterator it = PlaybillList.iterator();
                            while (it.hasNext()) {
                                Element el = (Element) it.next();
                                String text = el.elementText("PlayDay");
                                Element element = el.element("PlayLists");
                                List list = element.elements("Play");
                                int i = 1;
                                if (list != null && list.size() > 0) {
                                    Iterator iterator = list.iterator();
                                    while (iterator.hasNext()) {
                                        Playbill pb = new Playbill();
                                        Element nodes = (Element) iterator.next();
                                        //去掉节目名称中的英文单引号
                                        String pName = nodes.elementText("PlayName").replaceAll("'", "");
                                        String playShellID = nodes.elementText("PlayShellID");
                                        String playVodID = nodes.elementText("PlayVodID");

                                        String stt = nodes.elementText("StartTime");
                                        String edt = nodes.elementText("EndTime");
                                        if (stt != null && stt.length() == 16) {
                                            stt = sdf2.format(sdf.parse(stt));
                                        }
                                        if (stt != null && stt.length() == 19) {
                                            stt = sdf2.format(sdf1.parse(stt));
                                        }
                                        if (edt != null && edt.length() == 16) {
                                            edt = sdf2.format(sdf.parse(edt));
                                        }
                                        if (edt != null && edt.length() == 19) {
                                            edt = sdf2.format(sdf1.parse(edt));
                                        }

                                        String seqRank = String.valueOf(i);
                                        if (seqRank.length() == 1) {
                                            seqRank = "00" + seqRank;
                                        } else if (seqRank.length() == 2) {
                                            seqRank = "0" + seqRank;
                                        }
                                        seqRank = contentId + text + seqRank;

                                        pb.setRanking(String.valueOf(i));
                                        pb.setSequence(seqRank);
                                        pb.setPlayShellID(playShellID);
                                        pb.setPlayVodID(playVodID);
                                        pb.setStartTime(stt);
                                        pb.setEndTime(edt);
                                        pb.setProgram(pName);
                                        pb.setPlayDay(text);
                                        pb.setContentId(contentId);
                                        dbpbs.add(pb);

                                        i++;
                                    }
                                }
                            }
                            if (dbpbs.size() > 0) {
                            	playbillService.saveAll(dbpbs);
                            }
                        }
                    } catch (DocumentException e) {
                        log.error("内容同步主流程|内容:" + c.getContentId() + "|Playbill parse error", e);
                    }

                } else {
                    log.error("内容同步主流程|内容:" + c.getContentId() + "|Playbill path:" + path + " do not exist");
                }
            }
        } catch (Exception e) {
            log.error("内容同步主流程|内容:" + c.getContentId() + "|节目单入库失败", e);
        }
	}
}
